package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.ClientReqPara;
import com.lst.entity.response.ClientResPara;
import com.lst.model.MstClient;
import com.lst.service.MstClientService;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: BindMobileServlet
 * @Description: 绑定手机
 * @author zhanghl
 * @date 2016年11月28日 上午10:25:03
 * 
 */
public class BindMobileServlet extends BaseServlet {

    private static final long serialVersionUID = 1L;

    private MstClientService mstClientService = ctx
	    .getBean(MstClientService.class);

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
	doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	Date startDate = new Date();

	ClientResPara resPara = new ClientResPara();

	try {
	    ClientReqPara reqPara = getReqPara(request);

	    if (isCheckField(reqPara, resPara)) {
		Integer userid = Integer.valueOf(reqPara.getUserid());
		String mobileno = reqPara.getMobileno();
		String password = reqPara.getPassword();
		
		Map<String, Object> reqMap = new HashMap<String, Object>();

		reqMap.put("mobileno", mobileno);
		reqMap.put("enabled", true);
		reqMap.put("isDeleted", false);

		List<MstClient> list = mstClientService.queryByList(reqMap,new PageBounds());
		
		if(list!=null&&list.size()>0){
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_C000006);
		}else{
			MstClient mstclient = new MstClient();
			mstclient.setId(userid);
			mstclient.setMobileno(mobileno);
			mstclient.setPassword(password);
			mstclient.setUpdatedate(startDate);
			mstclient.setUpdateuser(userid);

			mstClientService.updateByPrimaryKeySelective(mstclient);

			MstClient mc = mstClientService.selectByPrimaryKey(userid);

			String mobile = mobileno.replaceAll("(\\d{3})\\d{4}(\\d{4})",
				"$1****$2");

			mc.setMobile(mobile);

			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
			resPara.setMstClient(mc);
		}

	    }

	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0001);

	    logger.error("doPost error: ", e);
	}

	try {
	    response.setContentType("application/json;charset=utf-8");
	    response.setHeader("pragma", "no-cache");
	    response.setHeader("cache-control", "no-cache");

	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		    .excludeFieldsWithoutExposeAnnotation().serializeNulls()
		    .create();
	    String jsonResult = gson.toJson(resPara);

	    PrintWriter out = response.getWriter();
	    out.print(jsonResult);
	    out.flush();
	    out.close();

	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0003);

	    logger.error("doPost error: ", e);
	}
    }

    @Override
    public ClientReqPara getReqPara(HttpServletRequest request) {

	ClientReqPara reqPara = null;

	try {
	    BaseRequest baseRequest = RequestUtils.getRequestPara(request,
		    new ClientReqPara());

	    reqPara = (ClientReqPara) baseRequest.clone();

	    String userid = request.getParameter("userid");
	    String mobileno = request.getParameter("mobileno");
	    String password = request.getParameter("password");

	    logger.info("reqPara userid: " + userid);
	    logger.info("reqPara mobileno: " + mobileno);
	    logger.info("reqPara password: " + password);

	    reqPara.setUserid(userid);
	    reqPara.setMobileno(mobileno);
	    reqPara.setPassword(password);

	} catch (Exception e) {
	    logger.error("getReqPara error: ", e);
	}

	return reqPara;
    }

    @Override
    public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
	boolean flag = false;

	ClientReqPara bmReqPara = (ClientReqPara) reqPara;
	ClientResPara bmResPara = (ClientResPara) resPara;

	if (StringUtils.isEmpty(bmReqPara.getUserid())) {
	    bmResPara.setCode(CommCode.M_A000000);
	    bmResPara.setMessage(CommCode.M_P000002);
	}else if (StringUtils.isEmpty(bmReqPara.getMobileno())) {
	    bmResPara.setCode(CommCode.M_A000000);
	    bmResPara.setMessage(CommCode.M_P000004);
	}else if (StringUtils.isEmpty(bmReqPara.getPassword())) {
	    bmResPara.setCode(CommCode.M_A000000);
	    bmResPara.setMessage(CommCode.M_C000002);
	} else {
	    flag = true;
	}

	return flag;
    }
}
